package web;

import ejb.Articolo;
import ejb.BusinessLogic;
import ejb.Utente;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Danilo
 */
public class ModificaAmministratore extends HttpServlet {

    @EJB
    private BusinessLogic businessLogic;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String perm = request.getParameter("aggiorna");
        String amm = request.getParameter("operazione_amm");
        String modifica = request.getParameter("modifica");
        String modifica2 = request.getParameter("modifica2");
        String elim = request.getParameter("elimina");
        if (amm != null) {
            if (amm.equals("1")) {      /* Prima Conferma utente */
                List<Utente> u2 = businessLogic.estraiNuoviUtenti();
                if (u2.isEmpty()) {
                    out.println("Non sono presenti utenti in attesa della conferma della registrazione.");
                } else {
                    out.println("<form>Nuovi Utenti in attesa di conferma registrazione");
                    out.println("<table>");
                    out.println("<tr>");
                    out.println("<td>Username</td><td>Cognome</td><td>Nome</td><td>Flag</td>");
                    out.println("</tr>");
                    for (int i = 0; i < u2.size(); i++) {
                        out.println("<tr>");
                        out.println("<td>" + u2.get(i).getUsername() + "</td>"
                                + "<td>" + u2.get(i).getCognome() + "</td>"
                                + "<td>" + u2.get(i).getNome() + "</td>"
                                + "<td><select name='permessi" + u2.get(i).getId() + "'>"
                                + "<option value='0'>0</option>"
                                + "<option value='1' selected>1</option>"
                                + "<option value='2'>2</option>"
                                + "<option value='3'>3</option>"
                                + "</select></td>");
                        out.println("</tr>");
                    }
                    out.println("</table>");
                    out.println("<input name='aggiorna' type='submit'/>");
                    out.println("</form>");
                }
            } else if (amm.equals("2")) {       /* Gestione Permessi utente */
                List<Utente> u2 = businessLogic.listaUtenti();
                if (u2.isEmpty()) {
                    out.println("Non sono presenti utenti registrati.");
                } else {
                    out.println("<form>");
                    out.println("<table>");
                    out.println("<tr>");
                    out.println("<td>Username</td><td>Cognome</td><td>Nome</td><td>Flag</td>");
                    out.println("</tr>");
                    for (int i = 0; i < u2.size(); i++) {
                        out.println("<tr>");
                        out.println("<td>" + u2.get(i).getUsername() + "</td>"
                                + "<td>" + u2.get(i).getCognome() + "</td>"
                                + "<td>" + u2.get(i).getNome() + "</td>"
                                + "<td><select name='permessi" + u2.get(i).getId() + "'>");
                        int flag = u2.get(i).getFlag();
                        if (flag == 3) {
                            out.println(
                                    "<option value='0'>0</option>"
                                    + "<option value='1'>1</option>"
                                    + "<option value='2'>2</option>"
                                    + "<option value='3' selected>3</option>");
                        } else if (flag == 1) {
                            out.println(
                                    "<option value='0'>0</option>"
                                    + "<option value='1' selected>1</option>"
                                    + "<option value='2'>2</option>"
                                    + "<option value='3'>3</option>");
                        } else if (flag == 2) {
                            out.println(
                                    "<option value='0'>0</option>"
                                    + "<option value='1'>1</option>"
                                    + "<option value='2' selected>2</option>"
                                    + "<option value='3'>3</option>");
                        } else {
                            out.println(
                                    "<option value='0' selected>0</option>"
                                    + "<option value='1'>1</option>"
                                    + "<option value='2'>2</option>"
                                    + "<option value='3'>3</option>");
                        }

                        out.println("</select></td></tr>");
                    }
                    out.println("</table>");
                    out.println("<input name='aggiorna' type='submit'/>");
                    out.println("</form>");
                }
            } else if (amm.equals("3")) {       /* Inserimento Prodotto */
                RequestDispatcher rd = request.getRequestDispatcher("inserisci_prodotto.jsp");
                rd.include(request, response);
            } else if (amm.equals("4")) {       /* Elimina Prodotto */
                List<Articolo> a = businessLogic.listaArticoliOrdinati();
                out.println("<form>");
                out.println("<table class='azioni_amm'>");
                out.println("<tr class='title'>");
                out.println("<td>Foto</td><td>Tipo</td><td>Marca</td><td>Modello</td>"
                        + "<td>Taglia</td><td>Elimina</td>");
                out.println("</tr>");
                for (int i = 0; i < a.size(); i++) {
                    if (i % 2 == 0) {
                        out.println("<tr class='pari'>");
                    } else {
                        out.println("<tr class='dispari'>");
                    }
                    out.println("<td><img src='" + a.get(i).getFoto() + "'></img></td>"
                            + "<td>" + a.get(i).getTipo() + "</td>"
                            + "<td>" + a.get(i).getMarca() + "</td>"
                            + "<td>" + a.get(i).getModello() + "</td>"
                            + "<td>" + a.get(i).getTaglia() + "</td>"
                            + "<td> <input type='checkbox' name='check" + i + "'/></td>");
                    out.println("</tr>");
                }
                out.println("</table>");
                out.println("<input name='elimina' value='Elimina' type='submit'/>");
                out.println("</form>");

            }
            if (amm.equals("5")) {
                List<Articolo> a = businessLogic.listaArticoliOrdinati();
                out.println("<form>");
                out.println("<table class='azioni_amm'>");
                out.println("<tr class='title'>");
                out.println("<td>Foto</td><td>Tipo</td><td>Marca</td><td>Modello</td>"
                        + "<td>Taglia</td><td>Elimina</td>");
                out.println("</tr>");
                for (int i = 0; i < a.size(); i++) {
                    if (i % 2 == 0) {
                        out.println("<tr class='pari'>");
                    } else {
                        out.println("<tr class='dispari'>");
                    }
                    out.println("<td><img src='" + a.get(i).getFoto() + "'></img></td>"
                            + "<td>" + a.get(i).getTipo() + "</td>"
                            + "<td>" + a.get(i).getMarca() + "</td>"
                            + "<td>" + a.get(i).getModello() + "</td>"
                            + "<td>" + a.get(i).getTaglia() + "</td>"
                            + "<td> <input type='radio' name='radio' value='" + i + "'/></td>");
                    // + "<input type='hidden' name='id"+i+"' value='"+a.get(i).getId() +"'>");
                    out.println("</tr>");
                }
                out.println("</table>");
                out.println("<input name='modifica' value='modifica' type='submit'/>");
                out.println("</form>");

            }
        }

        if (perm != null) {
            List<Utente> u2 = businessLogic.listaUtenti();  //Prima era .estraiNuoviUtenti()
            for (int i = 0; i < u2.size(); i++) {
                String permesso = request.getParameter("permessi" + u2.get(i).getId());
                if (permesso != null) {
                    businessLogic.updatePermessi(permesso, u2.get(i).getId());
                }
            }
        }
        if (elim != null) {
            List<Articolo> a = businessLogic.listaArticoliOrdinati();
            int cont = 0;
            for (int i = 0; i < a.size(); i++) {
                String checkbox = request.getParameter("check" + i);
                if (checkbox != null) {
                    businessLogic.eliminaArticolo(a.get(i));
                    cont++;
                }
            }
            if (cont > 0) {
                out.println("<h3>Prodotti eliminati con successo</h3>");
            } else {
                out.println("<h3>Nessun prodotto selezionato</h3>");
            }
        }

        if (modifica != null) {
            RequestDispatcher rd = request.getRequestDispatcher("/ModificaProdotto");
            rd.include(request, response);
        }
        if (modifica2 != null) {
            String id = request.getParameter("id_mod");
            String marca = request.getParameter("marca");
            String modello = request.getParameter("modello");
            String tipo = request.getParameter("tipo");
            String taglia = request.getParameter("taglia");
            String disp = request.getParameter("disp");
            String pr_acquisto = request.getParameter("pr_acquisto");
            String pr_vendita = request.getParameter("pr_vendita");
            String path_f = request.getParameter("foto");
            String descr = request.getParameter("descr");

            String query = "UPDATE Articolo a SET  a.descrizione='" + descr + "', "
                    + "a.foto='" + path_f + "', "
                    + "a.marca='" + marca + "', "
                    + "a.modello='" + modello + "',"
                    + " a.prezzo_acquisto='" + pr_acquisto + "', "
                    + "a.prezzo_vendita='" + pr_vendita + "', "
                    + "a.quantita='" + disp + "', "
                    + "a.taglia='" + taglia + "', "
                    + "a.tipo='" + tipo + "' "
                    + "WHERE a.id='" + id + "'";
            int val = businessLogic.modificaArticolo(query);
            if (val > 0) {
                out.println("<h3>Prodotti modificato con successo</h3>");
            } else {
                out.println("<h3>Errore nella modifica del prodotto</h3>");
            }
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
